<?php

class LN_Palo {
	public $type;
	public $host;
	public $port;
	public $database;
	public $devMod;
	public $error;
	
	function __construct($type, $host, $port, $database, $method='GET', $devMod=false) {
		// type
		$this->type = $type;		
		// host
		$this->host = $host;		
		// method
		$this->method = $method;		
		// port
		$this->port = $port;
		// database
		$this->database = $database;
		// defMod
		$this->devMod = $devMod;
	}
	
	function _request ($path, $data) {
		if(is_array($data)) {
			foreach ($data as $k => $v) {
				$str[] = urlencode($k).'='.urlencode($v);
			}
			$data = implode('&', $str);
		}
		
		$fp = fsockopen($this->host, $this->port, $errno, $errstr, 30);
		if (!$fp) {
		    $ret = array(2, $errstr, $errno);
		} else {
		    $out  = "GET ".$path."?".$data." HTTP/1.1\r\n";
		    $out .= "Host: ".$this->host."\r\n";
		    $out .= "Connection: Close\r\n\r\n";
			$out .= $data."\r\n\r\n";
			
		    fwrite($fp, $out);
		    while (!feof($fp)) {
		        $ret.= fgets($fp, 128);
		    }
		    fclose($fp);
		}
		
		// ERROR - Reporting
		$header = $this->fetch_header($ret);
		$div = '<div style="border: 1px solid #000; padding: 10px">';
		$div .= nl2br($ret);
		$div .= '</div>';		
		$this->error = $div;
		
		$badRequestHeaders = array(
			'HTTP/1.1 400 Bad request'
		);
		if(in_array($header[0], $badRequestHeaders)) {
			$this->_bad_request($ret);
		}
				
		if($this->devMod) {
			echo $this->error;
		}
		
		return $ret;
	}
	
	function filter_array($arr, $pos, $sollWert) {
		foreach($arr as $key => $werte) {
			if($werte[$pos] == $sollWert) {
				$ret[$key] = $werte;
			}
		}
		return $ret;
	}
	
	function login ($user, $pass, $ttl_check=false) {
		if(!$this->login_checksession($ttl_check)) {
			$data = array(
				'user' => $this->database.'-'.$user,
				'password' => md5($pass)
			);
			$ret = $this->_request('/server/login', $data);
			$loginRet = $this->fetch_array($ret);
			
			$sid = $loginRet[0][0];
			$ttl = $loginRet[0][1];
			$sst = time();
			
			$_SESSION['LN_sid'] = $sid;
			$_SESSION['LN_ttl'] = $ttl;
			$_SESSION['LN_Session_Start'] = $sst;
			
			$dbId = $this->database_id();
			$_SESSION['LN_Database_Id'] = $dbId;
		}
	}	
	function login_checksession($ttl=false) {
		if(!$ttl) {
			$ttl = $_SESSION['LN_ttl'];
		}
		
		// Check - Session
		if(isset($_SESSION['LN_sid'])) {
			$sStart = $_SESSION['LN_Session_Start'];
			$sEnde  = $sStart + $ttl;
			
			if($sEnde > time()) {
				return true;
			}
			else {
				return false;
			}
		}
		else {
			return false;
		}
	}
	
	function show($arr) {
		echo '<pre>';
		print_r($arr);
		echo '</pre>';
	}
	
	function fetch_header($ret) {
		$ret = explode("\r\n\r\n", $ret);
		$header = explode("\r\n", $ret[0]);
		return $header;
	}	
	function fetch_body($ret) {
		$ret = explode("\r\n\r\n", $ret);
		$body = explode("\n", $ret[1]);
		return $body;
	}	
	function fetch_array($ret) {
		$body = $this->fetch_body($ret);
		foreach($body as $num => $data) {
			$return[$num] = explode(';', $data);
		}
		unset($return[count($return)-1]);
		return $return;
	}

	function http_request($path, $data=false) {
		$str[] = urlencode('sid').'='.urlencode($_SESSION['LN_sid']);
		if($data) {
			foreach ($data as $k => $v) {
				$str[] = urlencode($k).'='.urlencode($v);
			}
		}
		$data = implode('&', $str);
		
		$ret = $this->_request($path, $data);
		$req = $this->fetch_array($ret);
		
		return $req;
	}
	function request_array($path, $data=false) {
		$str[] = urlencode('sid').'='.urlencode($_SESSION['LN_sid']);
		$str[] = urlencode('database').'='.urlencode($_SESSION['LN_Database_Id']);
		if($data) {
			foreach ($data as $k => $v) {
				$str[] = urlencode($k).'='.urlencode($v);
			}
		}
		$data = implode('&', $str);
		
		$ret = $this->_request($path, $data);
		$req = $this->fetch_array($ret);
		
		return $req;
	}
	function database_id() {
		$ret = $this->http_request('/server/databases', array('show_normal' => 1));
		$db_id = $ret[0][0];
		return $db_id;
	}
	
	function _bad_request($ret) {
		$body = $this->fetch_array($ret);
		$erCode = $body[0][0];
		
		switch($erCode) {
			case 1015:
				unset($_SESSION['LN_sid']);
				$this->refresh_session();
				break;
				return true;
			case 1004:
				unset($_SESSION['connection']);
				header('location: ./');
				return true;
			case 2:
				return 'unknown Error';
			default:
				return false;
		}
	}
	
	function refresh_session() {
		$this->login(USER_NAME, USER_PASS);
	}
}

